﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Qianwei.Common.Extension
{
    public static class IDataReaderExtension
    {
        /// <summary>
        /// DataReader转换为实体
        /// </summary>
        /// <typeparam name="T">转换的类型</typeparam>
        /// <param name="reader">DataReader对象</param>
        /// <returns></returns>
        public static T DataReaderToObject<T>(this IDataReader reader) where T : new()
        {
            T obj = default(T);
            if (reader == null) return obj;
            DynamicBuilder<T> builder = DynamicBuilder<T>.CreateBuilder(reader);
            while (reader.Read())
            {
                obj = builder.Build(reader);
            }
            return obj;
        }

        /// <summary>
        /// DataReader转换为list
        /// </summary>
        /// <typeparam name="T">要转换的类型</typeparam>
        /// <param name="reader">DataReader对象</param>
        /// <returns></returns>
        public static IList<T> DataReaderToList<T>(this IDataReader reader) where T : new()
        {
            IList<T> list = new List<T>();
            if (reader == null) return list;
            DynamicBuilder<T> builder = DynamicBuilder<T>.CreateBuilder(reader);
            while (reader.Read())
            {
                list.Add(builder.Build(reader));
            }
            return list;
        }
    }
}
